<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="营业员" prop="clerkName">
        <el-input
          v-model="queryParams.clerkName"
          placeholder="请输入营业员"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店" prop="storeId">
        <el-select v-model="queryParams.storeId" filterable placeholder="请选择门店" clearable size="small">
          <el-option
            v-for="store in storeList"
            :label="store.storeName" :value="store.storeId" />
        </el-select>
      </el-form-item>
      <el-form-item label="黑名单" prop="blackGrade">
        <el-select v-model="queryParams.blackGrade" placeholder="请选择黑名单等级" clearable size="small">
          <el-option
            v-for="black in blackGradeOptions"
            :label="black.label" :value="black.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:clerk:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:clerk:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:clerk:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:clerk:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clerkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="clerkId" />
      <el-table-column label="营业员" align="center" prop="clerkName" />
      <el-table-column label="门店" align="center" prop="storeId" :formatter="storeFormatter" />
      <el-table-column label="黑名单等级" align="center">
        <template slot-scope="scope">
          <el-tag
            v-for="black in blackGradeOptions"
            v-if="scope.row.blackGrade === black.value"
            :type="black.type"
          >{{black.label}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:clerk:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:clerk:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改营业员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="营业员" prop="clerkName">
          <el-input v-model="form.clerkName" placeholder="请输入营业员名称" />
        </el-form-item>
        <el-form-item label="门店" prop="storeId">
          <el-select v-model="form.storeId"  filterable placeholder="请选择门店">
            <el-option
              v-for="store in storeList"
              :label="store.storeName" :value="store.storeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="黑名单" prop="blackGrade">
          <el-radio-group v-model="form.blackGrade" size="mini">
            <el-radio v-for="black in blackGradeOptions" style="margin-right: 1px"
                      :label="black.value" border>{{black.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClerk, getClerk, delClerk, addClerk, updateClerk, exportClerk } from "@/api/basic/clerk";
import {listStore} from "@/api/basic/store"

export default {
  name: "Clerk",
  components: {
  },
  data() {
    return {
      /*黑名单等级*/
      blackGradeOptions: [
        {value: null, label: '无',type: 'info'},
        {value: 1, label: '一级', type: 'success'},
        {value: 2, label: '二级', type: 'warning'},
        {value: 3, label: '三级', type: 'danger'}
      ],
      // 门店列表
      storeList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营业员管理表格数据
      clerkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clerkName: null,
        storeId: null,
        blackGrade: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getStoreList();
  },
  methods: {
    /** 门店规格转换 */
    storeFormatter(row) {
      for(let i = 0; i < this.storeList.length; i++) {
        if(row.storeId === this.storeList[i].storeId) {
          return this.storeList[i].storeName;
        }
      }
    },
    /** 查询门店列表 */
    getStoreList() {
      listStore().then(resp => {
        this.storeList = resp.rows;
      })
    },
    /** 查询营业员管理列表 */
    getList() {
      this.loading = true;
      listClerk(this.queryParams).then(response => {
        this.clerkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clerkId: null,
        clerkName: null,
        storeId: null,
        blackGrade: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clerkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加营业员管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clerkId = row.clerkId || this.ids
      getClerk(clerkId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改营业员管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.clerkId != null) {
            updateClerk(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClerk(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const clerkIds = row.clerkId || this.ids;
      this.$confirm('是否确认删除营业员管理编号为"' + clerkIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delClerk(clerkIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有营业员管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportClerk(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
